-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.mem.Allocator.free: also account for sentinel if it's a pointer to an array #23020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
change looks sus, needs a better writeup |
I believe the correct fix would be at Line 4308 in 5c44934
- return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];
+ return @as(cast_target, @ptrCast(slice))[0 .. (slice.len + @intFromBool(std.meta.sentinel(Slice) != null)) * @sizeOf(std.meta.Elem(Slice))]; |
agreed, cc #19984 |
The correct fix is deprecating |
Actually, I wasn't thinking clearly earlier. The change proposed by this PR is just plain incorrect. To free a pointer to an array |
Currently the only function that handles sentinel terminated slices properly is free. All other uses of mem.sliceAsBytes() in the allocator interface lack proper handling of a possible sentinel. This commit changes the Allocator interface to use @ptrCast() plus the new mem.absorbSentinel() instead. This also makes incorrectly passing a pointer to array to Allocator.free() a compile error. The proper function to free a pointer to an array is Allocator.destroy(). Reported-by: David Vanderson <[email protected]> References: ziglang#19984 References: ziglang#22706 References: ziglang#23020
Currently the only function that handles sentinel terminated slices properly is free. All other uses of mem.sliceAsBytes() in the allocator interface lack proper handling of a possible sentinel. This commit changes the Allocator interface to use @ptrCast() plus the new mem.absorbSentinel() instead. This also makes incorrectly passing a pointer to array to Allocator.free() a compile error. The proper function to free a pointer to an array is Allocator.destroy(). Reported-by: David Vanderson <[email protected]> References: ziglang#19984 References: ziglang#22706 References: ziglang#23020
Currently the only function that handles sentinel terminated slices properly is free. All other uses of mem.sliceAsBytes() in the allocator interface lack proper handling of a possible sentinel. This commit changes the Allocator interface to use @ptrCast() plus the new mem.absorbSentinel() instead. This also makes incorrectly passing a pointer to array to Allocator.free() a compile error. The proper function to free a pointer to an array is Allocator.destroy(). Reported-by: David Vanderson <[email protected]> References: ziglang#19984 References: ziglang#22706 References: ziglang#23020
Fixes #23019 by checking the sentinel if it's a pointer to an array